﻿<script type="text/javascript">   

var xmlObj;
var rt_html="";
var final_last_time;

if(!localStorage.getItem('last_time'))
{
	localStorage.setItem('last_time', '1970-01-01 00:00:00');
}
var last_time = localStorage.getItem('last_time')

if(!localStorage.getItem('remain'))
{
	localStorage.setItem('remain', '0');
}
var remain = parseInt(localStorage.getItem('remain'));

setTimeout(refreshinback, 2000);
function refreshinback()
{
	getXMLContent();
	chrome.browserAction.setBadgeText({text: String(remain)}); 
	setTimeout(refreshinback, 900000); //15min
}



function getDataByid(number)
{
	var itemx = xmlObj.getElementsByTagName("item")[number];
	var title, link, pubDate, description;
	for(var i=0; itemx.childNodes[i]!=null; i++)
	{
		if(itemx.childNodes[i].nodeName == 'title')
		{
			title = itemx.childNodes[i].textContent;
		}else if(itemx.childNodes[i].nodeName == 'link')
		{
			link = itemx.childNodes[i].textContent;
		}else if(itemx.childNodes[i].nodeName == 'pubDate')
		{
			pubDate = itemx.childNodes[i].textContent;
		}else if(itemx.childNodes[i].nodeName == 'content:encoded')
		{
			description = itemx.childNodes[i].textContent;
		}
	}
	rt_html += '<div>';
	rt_html += '<hr><div style="font-size: 20px;"><a href="'+ link +'" target="_blank">'+ (number+1) + '. ' + title +'</a></div>';
	rt_html += '<div style="font-size: 10px;">Published on: '+ pubDate + '</div><hr>';
	rt_html += '<div style="font-size: 12px;">' + description + '</div>';
	rt_html += '</div><br>';
	var str_array = pubDate.split(" ");
	var new_time = str_array[3] + '-' + month_trans(str_array[2]) + '-' + str_array[1] + ' ' + str_array[4];
	if(number == 0)
	{
		final_last_time = new_time;
	}
	last_time = localStorage.getItem('last_time');
	remain = parseInt(localStorage.getItem('remain'));
	if(new_time > last_time) //有新内容
	{
		remain++;
		if(remain > 10)
			remain = 10;
		localStorage.setItem('remain', String(remain));
	}
}

//获RSS内容，解析并封装。
function getXMLContent(){
	xmlObj=loadXmlFile("http://fengchangjian.com/?feed=rss2");
	var items=xmlObj.getElementsByTagName("item").length;

	rt_html = "";
	for(i=0;i<items;i++){
		getDataByid(i);
	}
	var last_time = localStorage.getItem('last_time')
	if(final_last_time > last_time)
	{
		last_time = final_last_time;
		localStorage.setItem('last_time', last_time);
	}
	return rt_html;
}

//加载XML文件并返回XML节点内容
function loadXmlFile(xmlFile){
	var xmlDom = null;
	if (window.ActiveXObject){	//IE
		xmlDom = new ActiveXObject('MSXML2.DOMDocument.3.0');
		xmlDom.async=false;
		xmlDom.load(xmlFile);
	}else if (document.implementation && document.implementation.createDocument){	//Chrome
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", xmlFile, false);
		xmlhttp.send(null);
		xmlDom = xmlhttp.responseXML;
//	alert(xmlhttp.responseText);
	}else{
		xmlDom = null;
	}
	return xmlDom;
}

function month_trans(st_month)
{
	if(st_month == 'Jan')
		return '01';
	else if(st_month == 'Feb')
		return '02';
	else if(st_month == 'Mar')
		return '03';
	else if(st_month == 'Apr')
		return '04';
	else if(st_month == 'May')
		return '05';
	else if(st_month == 'Jun')
		return '06';
	else if(st_month == 'Jul')
		return '07';
	else if(st_month == 'Aug')
		return '08';
	else if(st_month == 'Sep')
		return '09';
	else if(st_month == 'Oct')
		return '10';
	else if(st_month == 'Nov')
		return '11';
	else if(st_month == 'Dec')
		return '12';
}
</script> 


 